
<template>
  <div>
    <div class="content">
      <div style="margin-bottom:10px;text-align:right;padding-right:10px">
        <el-button type="primary"
          @click="_addItem">新增</el-button>
      </div>
      <div class="table-wrapper">
        <el-table :data="tableData"
          pagination
          border
          style="width: 100%">
          <el-table-column prop="templateNumber"
            label="模版">
          </el-table-column>
          <el-table-column prop="procedureA"
            label="工序1">
          </el-table-column>
          <el-table-column prop="procedureB"
            label="工序2">
          </el-table-column>
          <el-table-column prop=""
            label="操作">
            <template slot-scope="scope">
              <el-button type="text"
                @click="_modify(scope.row)">修改</el-button>
              <el-button type="text"
                @click="_delete(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div style="margin-top:10px;text-align:right;">
        <el-pagination @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[10, 20, 30, 50]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next"
          :total="total">
        </el-pagination>
      </div>
    </div>
    <el-dialog title="修改"
      :visible="dialogVisible"
      width="30%"
      :before-close="_handleClose">
      <el-form :model="formInfo"
        ref="formInfo">
        <el-form-Item label="模版号"
          prop="templateNumber"
          label-width="100px">
          <el-input v-model="formInfo.templateNumber"></el-input>
        </el-form-Item>
        <el-form-Item label="工序1"
          prop="procedureA"
          label-width="100px">
          <el-input v-model="formInfo.procedureA"></el-input>
        </el-form-Item>
        <el-form-Item label="工序2"
          prop="procedureB"
          label-width="100px">
          <el-input v-model="formInfo.procedureB"></el-input>
        </el-form-Item>
        <el-form-item>
          <div style="text-align:center">
            <el-button @click="_handleClose">取消</el-button>
            <el-button type="primary"
              @click="_addItemHandle">确定</el-button>
          </div>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>

import { getList, deleteItem, addItem } from './../../utils/apis/commonApi'

export default {
  name: 'sfManage',
  data () {
    return {
      formInfo: {},
      currentObj: null,
      title: '',
      dialogVisible: false,
      tableData: [],
      total: 0,
      pageSize: 10,
      currentPage: 1
    }
  },
  created () {
    this._getData()
  },
  methods: {

    _getData () {
      let params = {
        size: this.pageSize,
        current: this.currentPage
      }
      getList(params).then(res => {
        if (res.data.code === 200) {
          this.tableData = res.data.data.list
          this.total = res.data.data.total
          this.currentPage = res.data.data.current
          this.pageSize = res.data.data.size
        } else {
          this.$message.error(res.data.message)
        }
      })
    },
    _addItem () {
      this.dialogVisible = true
      this.title = '新增'
      this.currentObj = null
    },
    _modify (row) {
      this.dialogVisible = true
      this.title = '修改'
      this.currentObj = row
      this.$nextTick(() => {
        this.formInfo = JSON.parse(JSON.stringify(row))
      })
    },
    _delete (row) {
      let params = {
        id: row.id
      }
      deleteItem(params).then(res => {
        if (res.data.code === 200) {
          this.$message.success(res.data.message)
          this._getData()
        } else {
          this.$message.error(res.data.message)
        }
      })
    },
    _handleClose () {
      this.$refs.formInfo.resetFields()
      this.currentObj = null
      this.dialogVisible = false
    },
    _addItemHandle () {
      let params = {
        ...this.formInfo
      }
      if (this.currentObj !== null) {
        params.id = this.currentObj.id
      }
      addItem(params).then(res => {
        if (res.data.code === 200) {
          this.$message.success(res.data.message)
          this._handleClose()
          this._getData()
        } else {
          this.$message.error(res.data.message)
        }
      })
    },

    handleSizeChange (val) {
      this.pageSize = val
      this._getData()
    },
    handleCurrentChange (val) {
      this.currentPage = val
      this._getData()
    }
  }
}
</script>
<style lang="less" scoped>
.form-wrapper {
  line-height: 40px;
  margin-bottom: 15px;
  padding: 10px;
  background: #fff;
  .el-form-item {
    margin-bottom: 0;
  }
}
.content {
  background: #fff;
  padding: 20px 15px;
  /*height: calc(100vh - 290px);*/
  overflow-y: scroll;
  .tabs-wrapper {
  }
  .table-wrapper {
  }
}
</style>
<style lang="less">
.el-tabs__header {
  margin-bottom: 0;
}
</style>
